package com.adb.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.adb.entity.GameDraft;

public class GameDraftDao extends BaseDatabaseHelper {
	public static String tableName = "GameDraft";
	ContentValues contentValues = null;

	GameDraftDao(Context context, String name, CursorFactory cursorFactory,
			int version) {
		super(context, name, cursorFactory, version);
	}

	public GameDraftDao(Context context) {
		super(context);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		// 每次成功打开数据库后首先被执行
	}

	public void iniContentValues(GameDraft gameDraft) {
		// 修改数据库,增加字段,必须修改这里
		contentValues = new ContentValues();
		contentValues.put("id", gameDraft.id);
		contentValues.put("avID", gameDraft.avID);
		contentValues.put("num", gameDraft.num);
		contentValues.put("draft", gameDraft.draft);

	}

	/**
	 * 添加数据
	 */
	public long insert(GameDraft gameDraft) {
		SQLiteDatabase db = getWritableDatabase();// 获取可写SQLiteDatabase对象
		iniContentValues(gameDraft); // ContentValues类似map，存入的是键值对
		long tmp = db.insert(tableName, null, contentValues);
		return tmp;
	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 */
	public void delete(int id) {
		SQLiteDatabase db = getWritableDatabase();
		db.delete(tableName, "id=?", new String[] { id + "" });
	}

	/**
	 * 更新记录的，跟插入的很像
	 */
	public void update(GameDraft gameDraft) {
		SQLiteDatabase db = getWritableDatabase();
		iniContentValues(gameDraft); // 初始化键值对
		db.update(tableName, contentValues, "id=?", new String[] { gameDraft.id
				+ "" });
	}

	/**
	 * 查询所有数据
	 * 
	 * @return Cursor
	 */
	public Cursor select() {
		String sql = "select * from " + tableName;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, new String[] {});
		return cursor;
	}

}
